package wp.ggt.com.wpview.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import wp.ggt.com.wpview.R;
import wp.ggt.com.wpview.bean.AppInfo;

/**
 * Created by KF on 2016/3/15.
 */
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {

    private final LayoutInflater from;
    private List<AppInfo> datas;

    public RecycleAdapter(Context context) {
        from = LayoutInflater.from(context);
    }

    public RecycleAdapter(Context context, List<AppInfo> datas) {
        from = LayoutInflater.from(context);
        this.datas = datas;
    }

    public void setDatas(List<AppInfo> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MyViewHolder(from.inflate(R.layout.recycleinfo, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.iv.setImageDrawable(datas.get(position).getIcon());
        holder.tv.setText(datas.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return datas != null ? datas.size() : 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv;
        private TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_recycle);
            tv = (TextView) itemView.findViewById(R.id.tv_recycle);
        }
    }
}
